﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CoVanHocTapData.Entities;
using CoVanHocTapData.Concretes;
using CoVanHocTapProject.Infrastructure;
using CoVanHocTapData.Infrastructure;
using System.Web.Security;

namespace CoVanHocTapProject.Controllers
{
    public class TrangChuController : Controller
    {
        #region repos
        private ISinhVienRepository svRepos = new SinhVienRepository();
        #endregion

        //
        // GET: /TrangChu/

        public ActionResult Index()
        {
            return View();
        }


        public ActionResult VerticalMenu()
        {
            return View();
        }


        public ActionResult DangNhap()
        {
            return View();
        }

        [HttpPost]
        public ActionResult DangNhap(FormCollection model)
        {
            var login = Request.Params.AllKeys.FirstOrDefault(key => key.Equals("subDangNhap"));
            if (login != null)
            {
                string strTenDangNhap = model["txtTenDangNhap"];
                string strMatKhau = model["txtMatKhau"];
                if (!String.IsNullOrEmpty(strTenDangNhap) && !String.IsNullOrEmpty(strMatKhau))
                {
                    SinhVien svDN = svRepos.LaySVDangNhap(strTenDangNhap);
                    if (svDN == null)
                    {
                        throw new Exception(Publics.ClientMessageBox("MSSV hoặc mật khẩu sai"));
                    }
                    else if(svDN!=null && svDN.MatKhau!=EncryptPassword.encryptString(strMatKhau))
                        throw new Exception(Publics.ClientMessageBox("Sai mật khẩu"));
                    Publics.sinhVienDangNhap = svDN;
                    //return RedirectToAction("Index", "TrangChu");
                    Response.Redirect("/TrangChu/Index");
                    //return View();
                }
            }
            return View();
        }

        public ActionResult DangNhapThanhCong()
        {

            return View(Publics.sinhVienDangNhap);
        }

        public ActionResult XemThongTinCaNhan()
        {
            return View();
        }

        public ActionResult DoiMatKhau()
        {
            return View();
        }

        public ActionResult Thoat()
        {
            return View();
        }
    }
}
